var DocData={
    roleId:baseFn.getQueryString('roleId',true),
    userId:'',
    baseData:{},
    initBase:function(data){
        var _userData=data
        $('.userName').text(_userData.userName)
        $('.userLevel').text(_userData.userLevel)
        $('.userPhone').text(_userData.userPhone)
        $('.userGender').text(_userData.userGender==0?'男':'女')
        $('.js_userAccount').text(_userData.userAccount)
        $('.js_cardCount').text(_userData.cardCount)
        $('.js_couponCount').text(_userData.couponCount)
        $('.js_orderCost').text(_userData.orderCost)
        $('.js_createTime').text(_userData.createTimeStr)
        $('.js_userImg').css('background-img','url('+_userData.userImg+')')
        /**
         * userAccount：储值余额
            cardCount：卡项
            couponCount：优惠券
            orderCost：订单金额
            userGender：性别
         */
    },
    initPatients:function(data){
        var _data=data,_html=''
        for(var d1=0;d1<_data.length;d1++){
            _html+='<div class="cols20 rows zcformelm">\
                        <a href="javascript:void(0)" data-vals="'+_data[d1].patientId+'" class="font_blue">宝宝'+(d1+1)+'：'+_data[d1].patientName+' </a>\
                    </div>'
        }
        $('.js_patientlist').html(_html)

    },
    initOrderList:function(data){
        var _data=data,_html=''
    },
    imgChange:false

}
var roleList=[]
var _data=roleList
var _powerData=[];

var _upData={}
if($('.js_posAdd').length == 1){
    baseFn.rpSelect('.js_subclinic',{
        url:baseUrl.base+baseUrl.subclinic+'selectSubclinicList',
        idKey:'subclinicId',
        textKey:'subclinicName',
        selFn:function(){
        }
    })
    if(baseFn.getQueryString('roleId') != null){
        DocData.roleId=baseFn.getQueryString('roleId')
        _upData.roleId = DocData.roleId 
    }
    $.get(baseUrl.base+baseUrl.role+'selectRoleList',_upData,function(res){
        if(DocData.roleId){
            DocData.baseData=res.results.roleInfo
            $('input[name=roleName]').val(DocData.baseData.roleName)
            $('select[name=subclinic]').val(DocData.baseData.subclinicIds).trigger('change')
            $('textarea.js_roleDesc').val(DocData.baseData.roleDesc)
        }
        DocData.roleList = res.results.roleList
        initPowerList(DocData.roleList)
    },'json')
}

function initPowerList(data){
    var _data = data,_powerData=[],_tempLineRow=$('<div class="rows zcformelm"></div>')
    for(var i=0;i<_data.length;i++){
        var _menuId = _data[i].menuId;
        if(_powerData.every(function (val) {
            return val.menuId != _menuId      
        })){
            if(_menuId == 999){
                _powerData.unshift({
                    menuId:_menuId,
                    menuName:_data[i].menuName,
                    functions:[
                        {
                            functionId:_data[i].functionId,
                            functionName:_data[i].functionName,
                            ischeck:_data[i].ischeck
                        }
                    ]
                })
            }else{
                _powerData.push({
                    menuId:_menuId,
                    menuName:_data[i].menuName,
                    functions:[
                        {
                            functionId:_data[i].functionId,
                            functionName:_data[i].functionName,
                            ischeck:_data[i].ischeck
                        }
                    ]
                })
            }
        }else{
            for(var j=0;j<_powerData.length;j++){
                if(_powerData[j].menuId == _menuId){
                    _powerData[j].functions.push({
                        functionId:_data[i].functionId,
                        functionName:_data[i].functionName,
                        ischeck:_data[i].ischeck
                    })
                    break;
                }
            }
        }
    }
    // var _lineNum = Math.ceil(_powerData.length/5),_templineNum=1
    for(var k=0;k<_powerData.length;k++){
        if((k%5) == 0){
            var _lineRow = $('<div class="rows zcformelm"></div>')
            _tempLineRow=_lineRow
            if(_tempLineRow != ''){
                _tempLineRow.appendTo('.js_powerwrap')
            }
            
            
        }
        var _lineCols = $('<div class="cols4"></div>')
        var _elmwrap=$('<div class="zcformelm"></div>')
        var _titElm=$('<p class="checkbox_wrap"><input type="checkbox" value="'+_powerData[k].menuId+'" rel="off" class="checkBox checkall" />'+_powerData[k].menuName+'</p>')
        var _childElmWrap=$('<div class="js_powergroup powergroup"></div>')
        for(var m=0;m<_powerData[k].functions.length;m++){
            var _function = _powerData[k].functions[m];
            var _childElm=$('<p class="checkbox_wrap"><input type="checkbox" value="'+_powerData[k].menuId+','+_function.functionId+'" rel="off" class="checkBox checkelm" />'+_function.functionName+'</p>')
            if(_function.ischeck == 1){
                _childElm.find('.checkBox').addClass('checkBox_checked')
                _titElm.find('.checkall').addClass('checkBox_checked')
            }
            _childElm.appendTo(_childElmWrap)
        }
        _titElm.appendTo(_elmwrap)
        _childElmWrap.appendTo(_elmwrap)
        _elmwrap.appendTo(_lineCols)
        _lineCols.appendTo(_tempLineRow)
    }
    $('.checkelm').on('click',function(){
        var $this = $(this),_ischeck=0
        var _checkboxData=$this.val().split(',')
        var _parent = $this.parents('.js_powergroup')
        if($this.hasClass('checkBox_checked')){
            $this.removeClass("checkBox_checked");
            _ischeck = 0
        }else{
            $this.addClass('checkBox_checked')
            _parent.siblings('.checkbox_wrap').find('.checkall').addClass('checkBox_checked')
            _ischeck = 1
        }
        for(var x in _data){
            if(_data[x].menuId==_checkboxData[0] && _data[x].functionId==_checkboxData[1]){
                _data[x].ischeck = _ischeck
            }
            if(baseFn.notNull(DocData.roleId)){
                _data[x].roleId = DocData.roleId
            }
        }

        
        // if(_parent.find('.checkBox').length == _parent.find('.checkBox_checked').length){
        //     _parent.siblings('.checkbox_wrap').find('.checkall').addClass('checkBox_checked')
        // }
        if(_parent.find('.checkBox_checked').length == 0){
            _parent.siblings('.checkbox_wrap').find('.checkall').removeClass('checkBox_checked')
        }
        //console.log(DocData.powerData)
    })
    $('.checkall').on('click',function(){
        var $this = $(this),_ischeck=0
        var _checkboxData=$this.val()
        if($this.hasClass('checkBox_checked')){
            $this.removeClass("checkBox_checked");
            $this.parent('.checkbox_wrap').siblings('.js_powergroup').find('.checkelm').removeClass('checkBox_checked')
            _ischeck = 0
        }else{
            $this.addClass('checkBox_checked')
            $this.parent('.checkbox_wrap').siblings('.js_powergroup').find('.checkelm').addClass('checkBox_checked')
            _ischeck = 1
        }
        for(var y in _data){
            if(_data[y].menuId == _checkboxData){
                _data[y].ischeck = _ischeck
            }
        }
        // console.log(DocData.powerData)
    })
}

$('.edit_ok').on('click',function(){
    var _upData={
        roleName:$('input.js_roleName').val(),
        roleDesc:$('textarea.js_roleDesc').val(),
        subclinicIds:$('select.js_subclinic').val(),
        roleList:JSON.stringify(DocData.roleList)
    }
    var _txt = '成功增加角色'
    if(DocData.roleId){
        _upData.roleId= DocData.roleId
        _txt = '成功修改角色'
    }
    $.post(baseUrl.base+baseUrl.role+'saveRole',_upData,function(res){
        if(res.code == '0'){
            baseFn.infoDialog(_txt)
        }else{
            baseFn.infoDialog(res.msg)
        }
    },'json')
})
if($('.js_rolelist').length == 1){
//角色列表
var role = baseFn.rpDataTables('.js_rolelist',{
    ajUrl:baseUrl.base+baseUrl.role+'selectSysRoleList',
    ajDataSrc:"results",
    ajType:'get',
    select:false,
    scrollY: windowOffsetData.tableHeight(),
    autoWidth:false,
    columns:[
        {"data":'roleName',defaultContent:'无',width:'10%',className:''},
        {"data":"roleDesc",width:'65%',defaultContent:'无',className:'lh_normal'},
        {"data":"roleId",defaultContent:'无',width:'15%',render:function(data,type,row, meta){
            return '<a href="javascript:void(0)" class="datatablebtn datatablebtnRed remove" data-vals="'+data+'">删除</a> <a href="/xrtn-manager/role/gotoAddRole?roleId='+data+'" class="datatablebtn detail" data-vals="'+data+'">详情</a>'
        }}
    ],
    initComplete:function(){},
    drawCallback: function( settings ) {
        // // alert( '表格重绘了' );
        $('.datatablebtn.remove').off('click').on('click',function(){
            var _tr = $(this).parents('tr'),ids = $(this).data('vals')        
            baseFn.dialog({
                title: '提示',
                content: '确定删除该角色？',
                ok: function(){
                    // baseFn.infoDialog('删除成功！')
                    $.post(baseUrl.base+baseUrl.role+'deleteRole',{roleId:ids},function(res){
                        if(res.code == '0'){
                            role.row(_tr).remove().draw()  
                        }
                        baseFn.infoDialog(_txt)
                    },'json')
                },
                cancel:function() {}
            })
        })
    }
})
}
//表格自适应
window.onresize = function(){
    windowOffsetData.refresh();
    if(baseFn.notNull(role)){
        $(role.table().body()).parents('.dataTables_scrollBody').css('max-height',windowOffsetData.tableHeight());
        role.draw(false);
    }
}

